package org.com.app.knock.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.com.app.knock.view.AreaItemView;
import org.com.app.knock.view.LoadingView;
import org.json.JSONArray;
import org.json.JSONObject;

import z.lib.base.BaseAdapter;
import z.lib.base.BaseItem;
import z.lib.base.BaseServiceCallBack;
import z.lib.base.BaseUtils;
import z.lib.base.BaseUtils.API;
import z.lib.base.BaseView;
import z.lib.base.CommonAndroid;
import z.lib.base.LogUtils;
import z.lib.base.callback.RestClient.RequestMethod;
import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.acv.libs.nbase.BaseFragment;
import com.knock.app.R;

public class AreaFragment extends BaseFragment implements AnimationListener {
	protected static final String TAG = "AreaFragment";
	private View views;
	ListView listView;
	private BaseAdapter baseAdapter;
	TextView txt_search;
	ArrayList<String> list_result = null;
	private LoadingView loading;
	public AreaFragment() {
		super();
	}

	@Override
	public void setUpFragment(View view) {
		this.views = view;
		listView =(ListView) views.findViewById(R.id.listview_main);
		loading = CommonAndroid.getView(view, R.id.loading);
		if( "".equals(RegisterFragment.response_)){
			search2();
		}else{
			setData(RegisterFragment.response_);
		}
	}

	@Override
	public int layoytResurce() {
		return R.layout.area;
	}
	
	@Override
	public void onClick(View v) {
		
	}

	private void search2() {
		// TODO Auto-generated method stub
		HashMap<String, String> params = new HashMap<String, String>();
		BaseUtils.execute(getActivity(), RequestMethod.POST, API.API_DATA, params, callback);
	}
	
	private BaseServiceCallBack callback = new BaseServiceCallBack() {

		public void onStart() {
			CommonAndroid.showView(true, loading);
			CommonAndroid.hiddenKeyBoard(getActivity());
		};

		public void onError(String message) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				LogUtils.e(TAG,"err=="+message);
			}
		};

		public void onSucces(String response) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				setData(response);
			}
		};
	};
	
	private void setData(String response){
		JSONObject mainJsonObject;
		List<BaseItem> baseItems = new ArrayList<BaseItem>();
		baseItems.clear();
		try {
			mainJsonObject = new JSONObject(response);
			JSONArray array = mainJsonObject.getJSONObject("data").getJSONArray("countries");
			for(int i =0; i < array.length();i++){
				baseItems.add(new BaseItem(array.getJSONObject(i)));
			}
			LogUtils.e(TAG,"setdata===="+mainJsonObject.toString());
			addView(baseItems);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void addView(List<BaseItem> baseItems) {
		// TODO Auto-generated method stub
        baseAdapter = new BaseAdapter(getActivity(), new ArrayList<Object>()) {
			@Override
			public BaseView getView(Context context, Object data) {
				final AreaItemView phonecodeListItemView = new AreaItemView(context);
				return phonecodeListItemView;
			}
		};
		baseAdapter.clear();
		baseAdapter.addAllItems(baseItems);
		baseAdapter.notifyDataSetChanged();
		listView.setOnItemClickListener(new OnItemClickListener() {
			@SuppressWarnings("unchecked")
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				BaseItem baseItem = (BaseItem) parent.getItemAtPosition(position);
				RegisterFragment.countryCode = baseItem.getString("idCountry");//countryCode
				getActivity().finish();
			}
		});
		
		listView.setAdapter(baseAdapter);
	}

	protected void addFriend(String friend_id) {
		
	}

	@Override
	public void onAnimationStart(Animation animation) {

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		mHandle.sendEmptyMessage(0);
	}

	@Override
	public void onAnimationRepeat(Animation animation) {

	}
}